﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MyCalendar.Utils;
using System.Diagnostics;

namespace MyCalendar.UIcomponent
{
    public partial class MacaCalendar : UserControl
    {
        DateTime mDateTime;

        DateTimeManager mDateTimeManager;

        public MacaCalendar (DateTime pDateTime)
        {
            mDateTime = pDateTime;
            mDateTimeManager = new DateTimeManager (mDateTime);
            InitializeComponent ();

            cGrid.DataContext = mDateTimeManager;
            PutOneMonthIntoCalendar (mDateTime);
        }

        public void PutOneDayIntoCalendar (DateTime pDateTime)
        {
            DateTimeTool _Tool = new DateTimeTool (pDateTime);
            CalendarPosition _Position = _Tool.ToCalendarPosition ();
            PutOneDayIntoCalendar (pDateTime,_Position.Row,_Position.Column);
        }

        public void PutOneDayIntoCalendar (DateTime pDateTime,int pRow, int pColumn)
        {
            MyDay mMyDay = new MyDay ();
            PutOneDayIntoCalendar (mMyDay, pDateTime, pRow, pColumn);
        }

        public void PutOneDayIntoCalendar (MyDay pMyDay,DateTime pDateTime, int pRow, int pColumn)
        {

            pMyDay.myDateText.Text = pDateTime.Day.ToString ();
            pMyDay.SetValue (Grid.RowProperty, pRow);
            pMyDay.SetValue (Grid.ColumnProperty, pColumn);
            cMyCalendar.Children.Add (pMyDay);
        }

        public void PutFirstWeekHelper (DateTime pDateTime, int pDay)
        {
            for (int i = 1; i <= pDay; i++)
            {
                Brush _BackColor = new SolidColorBrush (Color.FromArgb (255, 30, 30, 30));
                Brush _TextColor = new SolidColorBrush (Color.FromArgb (255, 100, 100, 100));

                PutOneDayIntoCalendar (new MyDay (_BackColor, _TextColor), pDateTime.AddDays (i * (-1)), 1, pDay + 1 - i);
            }
  
        }

        public void PutOneDayIntoCalendarFirstWeek (DateTime pDateTime)
        {
            switch (pDateTime.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    
                    PutFirstWeekHelper (pDateTime, 1);
                    break;

                case DayOfWeek.Tuesday:
                    PutFirstWeekHelper (pDateTime, 2);
                    break;

                case DayOfWeek.Wednesday:
                    PutFirstWeekHelper (pDateTime, 3);
                    break;

                case DayOfWeek.Thursday:
                    PutFirstWeekHelper (pDateTime, 4);
                    break;

                case DayOfWeek.Friday:
                    PutFirstWeekHelper (pDateTime, 5);
                    break;

                case DayOfWeek.Saturday:
                    PutFirstWeekHelper (pDateTime, 6);
                    break;

                default:
                    break;
            }
        }

        public void PutLastWeekHelper (DateTime pDateTime, int pDay)
        {
            DateTimeTool _Tool = new DateTimeTool (pDateTime);
            CalendarPosition _Position = _Tool.ToCalendarPosition ();
            int _Column = _Position.Column+1;

            for (int i = 1; i <= pDay; i++)
            {
                Brush _BackColor = new SolidColorBrush (Color.FromArgb (255, 30, 30, 30));
                Brush _TextColor = new SolidColorBrush (Color.FromArgb (255, 100, 100, 100));

                PutOneDayIntoCalendar (new MyDay(_BackColor,_TextColor),pDateTime.AddDays (i), _Position.Row, _Column++);
            }

        }

        public void PutOneDayIntoCalendarLastWeek (DateTime pDateTime)
        {
            switch (pDateTime.DayOfWeek)
            {
                case DayOfWeek.Sunday:
                    PutLastWeekHelper (pDateTime, 6);
                    break;

                case DayOfWeek.Monday:

                    PutLastWeekHelper (pDateTime, 5);
                    break;

                case DayOfWeek.Tuesday:
                    PutLastWeekHelper (pDateTime, 4);
                    break;

                case DayOfWeek.Wednesday:
                    PutLastWeekHelper (pDateTime, 3);
                    break;

                case DayOfWeek.Thursday:
                    PutLastWeekHelper (pDateTime, 2);
                    break;

                case DayOfWeek.Friday:
                    PutLastWeekHelper (pDateTime, 1);
                    break;

                default:
                    break;
            }
        }

        public void PutOneMonthIntoCalendar (DateTime pDateTime)
        {
            for (int i = 1; i <= DateTimeTool.GetTotalDaysInThisMonth (pDateTime); i++)
            {
                DateTime _DateTime = new DateTime (mDateTime.Year, mDateTime.Month, i);
                if (i == 1)
                {
                    if (_DateTime.DayOfWeek != DayOfWeek.Sunday)
                    {
                        PutOneDayIntoCalendarFirstWeek (_DateTime);
                    }
                }
                if (i == DateTimeTool.GetTotalDaysInThisMonth (pDateTime))
                {
                    if (_DateTime.DayOfWeek != DayOfWeek.Saturday)
                    {
                        PutOneDayIntoCalendarLastWeek (_DateTime);
                    }
                }
                PutOneDayIntoCalendar (_DateTime);
            }
        }

        private void cGrid_ManipulationCompleted (object sender, ManipulationCompletedEventArgs e)
        {
            Debug.WriteLine ("cGrid_ManipulationCompleted");
            if (e.FinalVelocities.LinearVelocity.Y < -100)
            {
                mDateTime = mDateTime.AddMonths (1);
                mDateTimeManager.SetDateTime (mDateTime);
                cMyCalendar.Children.Clear ();
                PutOneMonthIntoCalendar (mDateTime);
            }
            else if (e.FinalVelocities.LinearVelocity.Y > 100)
            {
                mDateTime = mDateTime.AddMonths (-1);
                mDateTimeManager.SetDateTime (mDateTime);
                cMyCalendar.Children.Clear ();
                PutOneMonthIntoCalendar (mDateTime);
            }
        }

    }
}
